package com.ait.edu.service.impl;

import com.ait.edu.client.Ossclient;
import com.ait.edu.entity.Video;
import com.ait.edu.mapper.VideoMapper;
import com.ait.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private Ossclient ossclient;

    @Override
    public void removeVideo(String id) {
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",id);
        videoQueryWrapper.select("video_source_id");
        List<Video> videos = this.list(videoQueryWrapper);
        List<String> list = new ArrayList<>();

        for (Video item:videos) {
            String sourceId = item.getVideoSourceId();
            if (!StringUtils.isEmpty(sourceId)) {
                list.add(sourceId);
            }

        }
        if (list.size()>0){ossclient.delBatchVideo(list);}


        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        this.remove(wrapper);
    }
}
